/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UploadPanel.java
 *
 * Created on May 7, 2011, 12:55:56 AM
 */
package uploadphotobucket.view;

import com.photobucket.api.core.FileUploadProgressEvent;
import com.photobucket.api.core.IFileUploadProgressEventListener;
import java.awt.Cursor;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;
import javax.swing.table.DefaultTableModel;
import org.json.JSONException;
import org.json.JSONObject;
import uploadphotobucket.action.ActionPhotobucket;
import utils.Album;
import utils.Browser;
import utils.Utils;
import utils.ImageFilter;
import utils.ImagePreview;
import utils.Message;
import utils.SharedObject;
import utils.TextFilter;

/**
 *
 * @author banhbaochay
 */
public class UploadPanel extends javax.swing.JPanel {

    /** Creates new form UploadPanel
     * @param sharedMap
     * @param locale  
     */
    public UploadPanel(SharedObject sharedMap, Locale locale) {
        this.locale = locale;
        Message.initialize(locale);
        this.sharedMap = sharedMap;
        initData();
        initComponents();
    }// end constructor

    /**
     * Change language of form
     */
    public void changeLanguage() {
        ResourceBundle rs = ResourceBundle.getBundle("resourcebundle.UploadPanel", locale);
        showImageButton.setToolTipText(rs.getString("UploadPanel.showImageButton.toolTipText"));
        copyLinkURLButton.setToolTipText(rs.getString("UploadPanel.copyLinkURLButton.toolTipText"));
        copyLinkBlogButton.setToolTipText(rs.getString("UploadPanel.copyLinkBlogButton.toolTipText"));
        addButton.setToolTipText(rs.getString("UploadPanel.addButton.toolTipText"));
        removeButton.setToolTipText(rs.getString("UploadPanel.removeButton.toolTipText"));
        clearButton.setToolTipText(rs.getString("UploadPanel.clearButton.toolTipText"));
        uploadButton.setToolTipText(rs.getString("UploadPanel.uploadButton.toolTipText"));
        saveButton.setToolTipText(rs.getString("UploadPanel.saveButton.toolTipText"));
        photoLabel.setText(rs.getString("UploadPanel.photoLabel.text"));
        albumLabel.setText(rs.getString("UploadPanel.albumLabel.text"));
    }// end changeLanguage method

    /**
     * Call when locale is changed by another form
     * @param locale 
     */
    public void localeChanged(Locale locale) {
        this.locale = locale;
        Message.initialize(locale);
    }// end localeChanged method

    private void initData() {
        String oauthConsumerKey = (String) sharedMap.getOauthConsumerKey();
        String oauthConsumerSecret = (String) sharedMap.getOauthConsumerSecret();
        action = new ActionPhotobucket(oauthConsumerKey, oauthConsumerSecret);
        browser = (Browser) sharedMap.getBrowser();
        currentPath = (String) sharedMap.getPath();
        Vector dataUpload = new Vector();
        Vector columnUpload = new Vector();
        columnUpload.add("File to upload");
        columnUpload.add("Link URL");
        columnUpload.add("Link Blog-Forum");
        columnUpload.add("Status");
        uploadTableModel = new DefaultTableModel(dataUpload, columnUpload);

        comboBoxModel = new DefaultComboBoxModel();
        if (sharedMap.containsKey(SharedObject.ALBUM_MAP)) {
            albumMap = (Map) sharedMap.getAlbumMap();
            for (Object o : albumMap.keySet()) {
                String albumName = (String) o;
                comboBoxModel.addElement(albumName);
            }
        }// end if sharedMap.containsKey("album.map")
    }// end initData method

    public void reload() {
        if (sharedMap.containsKey("album.map")) {
            albumMap = (Map) sharedMap.getAlbumMap();
            for (Object o : albumMap.keySet()) {
                String albumName = (String) o;
                comboBoxModel.addElement(albumName);
            }
        }// end if sharedMap.containsKey("album.map")
    }// end reload method

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel1 = new javax.swing.JPanel();
        uploadBtnPanel = new javax.swing.JPanel();
        clearButton = new javax.swing.JButton();
        uploadButton = new javax.swing.JButton();
        addButton = new javax.swing.JButton();
        removeButton = new javax.swing.JButton();
        albumComboBox = new javax.swing.JComboBox();
        photoLabel = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        uploadTable = new javax.swing.JTable();
        albumLabel = new javax.swing.JLabel();
        copyBtnPanel = new javax.swing.JPanel();
        copyLinkURLButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();
        showImageButton = new javax.swing.JButton();
        copyLinkBlogButton = new javax.swing.JButton();
        numberTextField = new javax.swing.JTextField();

        setLayout(new java.awt.GridBagLayout());

        uploadBtnPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        clearButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resourcebundle/icons/empty.png"))); // NOI18N
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("resourcebundle/UploadPanel"); // NOI18N
        clearButton.setText(bundle.getString("UploadPanel.clearButton.text")); // NOI18N
        clearButton.setToolTipText(bundle.getString("UploadPanel.clearButton.toolTipText")); // NOI18N
        clearButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearButtonActionPerformed(evt);
            }
        });
        uploadBtnPanel.add(clearButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(6, 160, 116, -1));

        uploadButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resourcebundle/icons/upload.png"))); // NOI18N
        uploadButton.setText(bundle.getString("UploadPanel.uploadButton.text")); // NOI18N
        uploadButton.setToolTipText(bundle.getString("UploadPanel.uploadButton.toolTipText")); // NOI18N
        uploadButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                uploadButtonActionPerformed(evt);
            }
        });
        uploadBtnPanel.add(uploadButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(6, 237, 116, -1));

        addButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resourcebundle/icons/add.png"))); // NOI18N
        addButton.setText(bundle.getString("UploadPanel.addButton.text")); // NOI18N
        addButton.setToolTipText(bundle.getString("UploadPanel.addButton.toolTipText")); // NOI18N
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });
        uploadBtnPanel.add(addButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(6, 6, 116, -1));

        removeButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resourcebundle/icons/delete.png"))); // NOI18N
        removeButton.setText(bundle.getString("UploadPanel.removeButton.text")); // NOI18N
        removeButton.setToolTipText(bundle.getString("UploadPanel.removeButton.toolTipText")); // NOI18N
        removeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeButtonActionPerformed(evt);
            }
        });
        uploadBtnPanel.add(removeButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(6, 83, 116, -1));

        albumComboBox.setModel(comboBoxModel);
        albumComboBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                albumComboBoxItemStateChanged(evt);
            }
        });

        photoLabel.setText(bundle.getString("UploadPanel.photoLabel.text")); // NOI18N

        uploadTable.setModel(uploadTableModel);
        jScrollPane2.setViewportView(uploadTable);

        albumLabel.setText(bundle.getString("UploadPanel.albumLabel.text")); // NOI18N

        copyBtnPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        copyLinkURLButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resourcebundle/icons/copy.png"))); // NOI18N
        copyLinkURLButton.setText(bundle.getString("UploadPanel.copyLinkURLButton.text")); // NOI18N
        copyLinkURLButton.setToolTipText(bundle.getString("UploadPanel.copyLinkURLButton.toolTipText")); // NOI18N
        copyLinkURLButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                copyLinkURLButtonActionPerformed(evt);
            }
        });
        copyBtnPanel.add(copyLinkURLButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(242, 6, -1, -1));

        saveButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resourcebundle/icons/save.png"))); // NOI18N
        saveButton.setText(bundle.getString("UploadPanel.saveButton.text")); // NOI18N
        saveButton.setToolTipText(bundle.getString("UploadPanel.saveButton.toolTipText")); // NOI18N
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });
        copyBtnPanel.add(saveButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(256, 77, 110, -1));

        showImageButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resourcebundle/icons/view.png"))); // NOI18N
        showImageButton.setText(bundle.getString("UploadPanel.showImageButton.text")); // NOI18N
        showImageButton.setToolTipText(bundle.getString("UploadPanel.showImageButton.toolTipText")); // NOI18N
        showImageButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showImageButtonActionPerformed(evt);
            }
        });
        copyBtnPanel.add(showImageButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(6, 6, -1, -1));

        copyLinkBlogButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resourcebundle/icons/copy.png"))); // NOI18N
        copyLinkBlogButton.setText(bundle.getString("UploadPanel.copyLinkBlogButton.text")); // NOI18N
        copyLinkBlogButton.setToolTipText(bundle.getString("UploadPanel.copyLinkBlogButton.toolTipText")); // NOI18N
        copyLinkBlogButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                copyLinkBlogButtonActionPerformed(evt);
            }
        });
        copyBtnPanel.add(copyLinkBlogButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(468, 6, -1, -1));

        numberTextField.setEditable(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(13, 13, 13)
                        .addComponent(albumLabel)
                        .addGap(18, 18, 18)
                        .addComponent(albumComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(79, 79, 79)
                        .addComponent(photoLabel)
                        .addGap(18, 18, 18)
                        .addComponent(numberTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 540, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(50, 50, 50)
                        .addComponent(uploadBtnPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(70, 70, 70)
                        .addComponent(copyBtnPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(21, 21, 21)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(albumLabel)
                    .addComponent(albumComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(photoLabel)
                    .addComponent(numberTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(uploadBtnPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(38, 38, 38)
                .addComponent(copyBtnPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = -6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(51, 57, 16, 60);
        add(jPanel1, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void albumComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_albumComboBoxItemStateChanged
        // TODO add your handling code here:
        String albumName = (String) evt.getItem();
        Album album = (Album) albumMap.get(albumName);
        numberTextField.setText(Integer.toString(album.getPhotoCount()));
    }//GEN-LAST:event_albumComboBoxItemStateChanged

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
        // TODO add your handling code here:
        JFileChooser fc = new JFileChooser();
        fc.setAccessory(new ImagePreview(fc));
        fc.setCurrentDirectory(new File(currentPath));
        fc.setFileFilter(new ImageFilter());
        fc.setMultiSelectionEnabled(true);
        fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        int returnVal = fc.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File[] files = fc.getSelectedFiles();
            File dir = fc.getCurrentDirectory();
            currentPath = dir.getPath();
            for (File file : files) {
                Vector tmp = new Vector();
                tmp.add(new utils.File(file.getAbsolutePath()));
                tmp.add("");
                tmp.add("");
                tmp.add("Waiting");
                uploadTableModel.addRow(tmp);
            }
        }
}//GEN-LAST:event_addButtonActionPerformed

    private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
        // TODO add your handling code here:
        int[] rows = uploadTable.getSelectedRows();
        for (int i = rows.length - 1; i >= 0; i--) {
            uploadTableModel.removeRow(rows[i]);
        }
}//GEN-LAST:event_removeButtonActionPerformed

    private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearButtonActionPerformed
        // TODO add your handling code here:
        uploadTableModel.getDataVector().removeAllElements();
        uploadTableModel.fireTableDataChanged();
}//GEN-LAST:event_clearButtonActionPerformed

    private void uploadButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_uploadButtonActionPerformed
        // TODO add your handling code here:
        if (!sharedMap.containsKey(SharedObject.USER_JSON)) {
            JOptionPane.showMessageDialog(null, Message.NOT_PERMISSION(), "Error",
                    JOptionPane.ERROR_MESSAGE);
        } else {
            String albumName = (String) comboBoxModel.getSelectedItem();
            UploadWorker worker = new UploadWorker(albumName);
            worker.execute();
        }
}//GEN-LAST:event_uploadButtonActionPerformed

    private void copyLinkBlogButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_copyLinkBlogButtonActionPerformed
        // TODO add your handling code here:
        int[] rows = uploadTable.getSelectedRows();
        if (rows.length > 0) {
            String links = "";
            for (int row : rows) {
                String status = (String) uploadTableModel.getValueAt(row, 3);
                if (status.equals("Done")) {
                    links = links + (String) uploadTableModel.getValueAt(row, 2) + "\n";
                }
            }
            Utils.setClipboard(links);
            String message = (links.equals("")) ? Message.SELECT_ROW_FAILED_UPLOAD()
                    : Message.BLOG_COPY_CLIPBOARD_DONE();
            JOptionPane.showMessageDialog(null, message, "Message",
                    JOptionPane.INFORMATION_MESSAGE);
        } else {
            // no selected row
            JOptionPane.showMessageDialog(null, Message.NO_SELECT_ROW_COPY(), "Warning",
                    JOptionPane.WARNING_MESSAGE);
        }
}//GEN-LAST:event_copyLinkBlogButtonActionPerformed

    private void copyLinkURLButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_copyLinkURLButtonActionPerformed
        // TODO add your handling code here:
        int[] rows = uploadTable.getSelectedRows();
        if (rows.length > 0) {
            String links = "";
            for (int row : rows) {
                String status = (String) uploadTableModel.getValueAt(row, 3);
                if (status.equals("Done")) {
                    links = links + (String) uploadTableModel.getValueAt(row, 1) + "\n";
                }
            }
            String message = (links.equals("")) ? Message.SELECT_ROW_FAILED_UPLOAD()
                    : Message.URL_COPY_CLIPBOARD_DONE();
            Utils.setClipboard(links);
            JOptionPane.showMessageDialog(null, message, "Message",
                    JOptionPane.INFORMATION_MESSAGE);
        } else {
            // no selected row
            JOptionPane.showMessageDialog(null, Message.NO_SELECT_ROW_COPY(),
                    "Warning", JOptionPane.WARNING_MESSAGE);
        }
}//GEN-LAST:event_copyLinkURLButtonActionPerformed

    private void showImageButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showImageButtonActionPerformed
        // TODO add your handling code here:
        int[] rowArray = uploadTable.getSelectedRows();
        if (rowArray.length > 0) {
            for (int row : rowArray) {
                /*
                 * Get status of file upload at 4th column
                 */

                String status = (String) uploadTableModel.getValueAt(row, 3);
                if (status.equals("Done")) {
                    /*
                     * Get url of file upload at 2th column
                     */
                    String url = (String) uploadTableModel.getValueAt(row, 1);
                    try {
                        browser.browseURL(url);
                    } catch (UnsupportedOperationException e) {
                        JOptionPane.showMessageDialog(null, Message.NOT_SUPPORTED_BROWSER(), "Warning",
                                JOptionPane.WARNING_MESSAGE);
                    } catch (IOException ex) {
                        JOptionPane.showMessageDialog(null, ex.getMessage());
                    }
                }// end if status.equals("Done")
            }
        } else {
            JOptionPane.showMessageDialog(null, Message.NO_SELECT_ROW_SHOW(),
                    "Warning", JOptionPane.WARNING_MESSAGE);
        }// end if rowArray.length > 0
}//GEN-LAST:event_showImageButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        // TODO add your handling code here:
        ArrayList<String> urlList = getURLList();
        if (urlList.isEmpty()) {
            /* No line in uploadTable or no successful upload*/
            JOptionPane.showMessageDialog(null, Message.NO_DATA_SAVE(),
                    "Warning", JOptionPane.WARNING_MESSAGE);
        } else {
            JFileChooser fc = new JFileChooser();
            fc.setFileFilter(new TextFilter());
            fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
            int val = fc.showSaveDialog(null);
            if (val == JFileChooser.APPROVE_OPTION) {
                PrintWriter out = null;
                try {
                    File f = fc.getSelectedFile();
                    out = new PrintWriter(f);
                    for (String url : urlList) {
                        out.print(url);
                        out.print("\r\n");
                    }
                } catch (FileNotFoundException ex) {
                    JOptionPane.showMessageDialog(null, ex.getMessage());
                } finally {
                    out.close();
                    JOptionPane.showMessageDialog(null, Message.SAVE_DONE());
                }
            }
        }
    }//GEN-LAST:event_saveButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addButton;
    private javax.swing.JComboBox albumComboBox;
    private javax.swing.JLabel albumLabel;
    private javax.swing.JButton clearButton;
    private javax.swing.JPanel copyBtnPanel;
    private javax.swing.JButton copyLinkBlogButton;
    private javax.swing.JButton copyLinkURLButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField numberTextField;
    private javax.swing.JLabel photoLabel;
    private javax.swing.JButton removeButton;
    private javax.swing.JButton saveButton;
    private javax.swing.JButton showImageButton;
    private javax.swing.JPanel uploadBtnPanel;
    private javax.swing.JButton uploadButton;
    private javax.swing.JTable uploadTable;
    // End of variables declaration//GEN-END:variables
    private DefaultComboBoxModel comboBoxModel;
    /*
     * Map objects share from main frame and login panel
     */
    private SharedObject sharedMap;
    /*
     * albumMap receive from login panel. Map has key is album's name, value is an album type
     */
    private Map albumMap;
    private DefaultTableModel uploadTableModel;
    private ActionPhotobucket action;
    private Browser browser;
    private String currentPath;
    /*
     * count how many image upload successful
     */
    private Locale locale;

    /**
     * Get all URL of images which are uploaded successful
     * @return 
     */
    private ArrayList<String> getURLList() {
        ArrayList<String> list = new ArrayList<String>();
        int rowCount = uploadTableModel.getRowCount();
        if (rowCount > 0) {
            for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
                /* check status: column 4 */
                String status = (String) uploadTableModel.getValueAt(rowIndex, 3);
                if (status.equals("Done")) {
                    list.add((String) uploadTableModel.getValueAt(rowIndex, 1));
                }// end if status
            }// end for rowIndex
        }// end if rowCount > 0
        return list;
    }// end getURLList method

    /**
     * Class for solve freeze Swing when click upload button
     */
    class UploadWorker extends SwingWorker<Void, Void> {

        public UploadWorker() {
            super();
        }

        public UploadWorker(String albumName) {
            super();
            this.albumName = albumName;
        }

        @Override
        protected Void doInBackground() throws Exception {
            setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
            int rowCount = uploadTableModel.getRowCount();
            int countSuccess = 0;
            for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
                String statusFile = (String) uploadTableModel.getValueAt(rowIndex, 3);
                if (!statusFile.equals("Done")) {
                    utils.File file = (utils.File) uploadTableModel.getValueAt(rowIndex, 0);
//                    String filePath = uploadTableModel.getValueAt(rowIndex, 0).toString();
                    String filePath = file.getAbsolutePath();
                    JSONObject userJSON = (JSONObject) sharedMap.getUserJSON();
                    JSONObject uploadJSON = action.uploadMedia(filePath, albumName, userJSON, new FileUploadProgress(rowIndex));
                    if (uploadJSON.optString("status").equals("OK")) {
                        //upload successful
                        JSONObject content = uploadJSON.optJSONObject("content");
                        countSuccess++;
                        String url = content.optString("url");
                        String linkBlog = "[IMG]" + url + "[/IMG]";
                        // update url to table
                        uploadTableModel.setValueAt(url, rowIndex, 1);
                        // update link blog to table
                        uploadTableModel.setValueAt(linkBlog, rowIndex, 2);
                        // update status to table
                        uploadTableModel.setValueAt("Done", rowIndex, 3);
                    } else {
                        uploadTableModel.setValueAt("Failed", rowIndex, 3);
                    }// end if upload successfull
                }// end if file not upload

            }// end for rowIndex
            
            // Update photo count of album
            if (countSuccess > 0) {
                updateAlbumsMap(albumName, countSuccess);
            }
            JOptionPane.showMessageDialog(null, Message.UPLOAD_DONE());
            return null;
        }

        @Override
        protected void done() {
            setCursor(null);
        }
        private String albumName;

        /**
         * update photo_count to album which has name is albumName
         * @param albumName
         * @param countSuccess 
         */
        private void updateAlbumsMap(String albumName, int countSuccess) {
            Album album = (Album) albumMap.get(albumName);
            int oldPhotos = album.getPhotoCount();
            try {
                album.setPhotoCount(oldPhotos + countSuccess);
                numberTextField.setText(Integer.toString(oldPhotos + countSuccess));
            } catch (JSONException ex) {
                JOptionPane.showMessageDialog(null, Message.NOT_SET_PHOTO_COUNT(),
                        "Warning", JOptionPane.WARNING_MESSAGE);
            }
        }
    }// end UploadWorker

    /**
     * Class process when uploading image
     */
    class FileUploadProgress implements IFileUploadProgressEventListener {

        public FileUploadProgress() {
        }

        public FileUploadProgress(int rowIndex) {
            this.rowIndex = rowIndex;
        }

        public void fileUploadProgressUpdate(FileUploadProgressEvent fupe) {
            uploadTableModel.setValueAt(fupe.getPercentComplete() + "%", rowIndex, 3);
        }
        private int rowIndex;
    }// end FileUploadProgress class
}
